import jwt from "jsonwebtoken";
import { serverConfig } from "../config";
//  todo 校验方式 后续视情况修改为RS256
function sign(data: any) {
  return jwt.sign(data, serverConfig.jwt.jwt_secret as string, {
    expiresIn: serverConfig.jwt.jwt_expirt,
  });
}

function verify(token: string) {
  try {
    const decoded = jwt.verify(token, serverConfig.jwt.jwt_secret as string);
    return {
      data: decoded,
      error: null,
    };
  } catch (err) {
    return {
      data: null,
      error: err,
    };
  }
}

export { sign, verify };
